package ch.sv7.tool.dbmanager.db.fixbuilder.sybase;

import java.util.Iterator;

import ch.sv7.tool.dbmanager.Context;
import ch.sv7.tool.dbmanager.db.TableSchema;
import ch.sv7.tool.dbmanager.db.dialect.Dialect;
import ch.sv7.tool.dbmanager.db.dialect.sybase.SybaseDialect;
import ch.sv7.tool.dbmanager.db.difference.AbstractColumnDifference;
import ch.sv7.tool.dbmanager.db.difference.ExtraColumnDifference;
import ch.sv7.tool.dbmanager.db.difference.SchemaDifference;
import ch.sv7.tool.dbmanager.db.fix.SchemaFix;
import ch.sv7.tool.dbmanager.db.fixbuilder.ExtraColumnFixBuilder;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class SybaseExtraColumnFixBuilder extends ExtraColumnFixBuilder {
	private Dialect dialect;
	
	public SchemaFix[] createFixes(SchemaDifference difference, Dialect dialect, Context context) {
		this.dialect = dialect;
		return super.createFixes(difference, dialect, context);
	}

	public boolean isPartionned(TableSchema schema) {
		return ((SybaseDialect)dialect).getPartitionNumber(schema) > 1;
	}
	
	public Iterator getIndexesToDrop(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getIndexesToDrop(columnDifference);
		}
		return columnDifference.getActualColumnSchema().getTable().getIndexes().iterator();
	}
	
	public Iterator getIndexesToCreate(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getIndexesToCreate(columnDifference);
		}
		ExtraColumnDifference extraColumnDifference = (ExtraColumnDifference)columnDifference;
		return extraColumnDifference.getExpectedTableSchema().getIndexes().iterator();
	}
	
	public Iterator getForeignKeyToDrop(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getForeignKeyToDrop(columnDifference);
		}
		Iterator result = columnDifference.getActualColumnSchema().getTable().getReferencedForeignKeys().iterator();
		return result;
	}
	
	public Iterator getForeignKeyToCreate(AbstractColumnDifference columnDifference) {
		if (!isPartionned(columnDifference.getActualTableSchema())){
			return super.getForeignKeyToDrop(columnDifference);
		}
		ExtraColumnDifference extraColumnDifference = (ExtraColumnDifference)columnDifference;
		Iterator result = extraColumnDifference.getExpectedTableSchema().getReferencedForeignKeys().iterator();
		return result;
	}
}
